#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class PalindromeList {
public:
    ListNode* findMid(ListNode* head) {
        ListNode* slow, * fast;
        slow = fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
    ListNode* reverse(ListNode* head) {
        ListNode* n1, * n2, * n3;
        n1 = NULL;
        n2 = head;
        n3 = head->next;
        while (n2) {
            n2->next = n1;
            n1 = n2;
            n2 = n3;
            if (n3)
                n3 = n3->next;
        }
        return n1;
    }
    bool chkPalindrome(ListNode* head) {
        ListNode* prev, * cur = head;
        ListNode* mid = findMid(head);
        ListNode* ret = reverse(mid);
        while (cur) {
            prev = cur;
            if (prev->next == mid)
                break;
            cur = cur->next;
        }
        prev->next = NULL;
        while (head && ret) {
            if (head->val != ret->val)
                return false;
            head = head->next;
            ret = ret->next;
        }
        return true;
    }
};